﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HCFramework.API.LINQ;

namespace C
{
    public class OrderController
    {
        public static void Insert(Order a)
        {
            a.Identity = Guid.NewGuid();
            a.CreatedDate = DateTime.Now;

            HCDataContext db = new HCDataContext();
            db.Orders.InsertOnSubmit(a);
            db.SubmitChanges();

            C.OrderHistoryController.Insert(a.OrderID, (E.OrderStatus)a.Status);
        }

        public static void Update(Order a, HCDataContext db)
        {
            db.SubmitChanges();
            C.OrderHistoryController.Insert(a.OrderID, (E.OrderStatus)a.Status);
        }

        public static void Delete(int id)
        {
            throw new Exception("Không cho phép xóa Order");

            //HCDataContext db = new HCDataContext();
            //Order a = db.Orders.Where(
            //    ik => ik.OrderID == id).First();
            //db.Orders.DeleteOnSubmit(a);
            //db.SubmitChanges();
        }

        public static Order GetByID(int id, HCDataContext db)
        {
            return db.Orders.Where(
                ik => ik.OrderID == id).FirstOrDefault();
        }

        public static Order GetByID(int id)
        {
            return GetByID(id, new HCDataContext());
        }

        public static decimal GetTotalPrice(int id)
        {
            Order order = GetByID(id);
            return order.Order_Products.Sum(ik => ik.Price * ik.Quantity);
        }

        public static Order Get(Guid identity)
        {
            return new HCDataContext().Orders.Where(
                ik => ik.Identity == identity).FirstOrDefault();
        }

        public static string GetStatusName(int orderStatus)
        {
            E.OrderStatus os = (E.OrderStatus)orderStatus;
            return E.Method.GetDisplayName_OrderStatus(os);
        }

        public static string GetDeliveryName(int deliveryType)
        {
            E.DeliveryType d = (E.DeliveryType)deliveryType;
            return E.Method.GetDisplayName_DeliveryType(d);
        }

        public static string GetPaymentMethodName(int paymentMethod)
        {
            E.PaymentMethod p = (E.PaymentMethod)paymentMethod;
            return E.Method.GetDisplayName_PaymentMethod(p);
        }

        public static string GetOrderCode(int orderId)
        {
            return "HD" + orderId.ToString("D7");
        }
    }
}